# Copyright 2007, 2008 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import gvn.cmdline
import gvn.util


helptext__gvn_revert = """\
revert: Restore pristine working copy file (undo most local edits).
usage: revert [-c CHANGE | PATH...]

  Revert the changes made to the paths changed in CHANGE or PATHS.
"""


def Handle_GvnRevert(ctx):
  targets = set(ctx.operands)
  if ctx.options.change is not None:
    # TODO(epg): Refuse to revert if changebranch is out of date?  Do
    # a snapshot here for the user?
    ctx.options.recursive = True
    ctx.options.gvn_options.append('recursive')
    for (path, state) in ctx.wc.change_state[ctx.options.change].paths.iteritems():
      # It seems odd to call get the DisplayPath here, but really it
      # isn't.  path is relative to the wc top, so directly passing
      # that to svn only works if current directory == wc top, which
      # is frequently not true.  DisplayPath localizes (fine, svn
      # expects that anyway) and converts to cwd-relative.
      targets.add(ctx.DisplayPath(path))
      # TODO: Don't forget we need to notice this kind of thing at snap time.
      # i.e. not here, but in ChangeBranch.Snapshot
    ctx.options.gvn_options.remove('change')
  return gvn.cmdline.RunSvnCommand(ctx, 'revert', sorted(targets))

gvn.cmdline.AddCommand('revert', Handle_GvnRevert, helptext__gvn_revert,
                       ['change', 'targets', 'recursive', 'quiet'],
                       {'change': 'changebranch ARG'})
